﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace CommonLibrary
{
    /// <summary>
    /// 字典扩展类
    /// </summary>
    public static class DicExt
    {
        /// <summary>
        /// 增加key/value
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddKeyValue(this Dictionary<string, object> dic, string key, object value)
        {
            if (dic != null && !string.IsNullOrWhiteSpace(key) && !dic.ContainsKey(key))
            {
                dic.Add(key, value);
            }
            else if (dic != null && !string.IsNullOrWhiteSpace(key) && dic.ContainsKey(key))
            {
                dic[key] = value;
            }
        }
    }

    /// <summary>
    /// Chart双缓冲
    /// </summary>
    public static class DoubleBufferChart
    {
        /// <summary>
        /// 使用双缓冲，解决 Chart 不闪烁
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="flag"></param>
        public static void DoubleBufferredChart(this Chart chart, bool flag)
        {
            var type = chart.GetType();
            PropertyInfo pi = type.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(chart, flag, null);
        }
    }

    /// <summary>
    /// Panel双缓冲
    /// </summary>
    public static class DoubleBufferPanel
    {
        /// <summary>
        /// 使用双缓冲，解决 Panel 不闪烁
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="flag"></param>
        public static void DoubleBufferredPanel(this Panel panel, bool flag)
        {
            var type = panel.GetType();
            PropertyInfo pi = type.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(panel, flag, null);
        }
    }
    /// <summary>
    /// DataGirdView 双缓冲
    /// </summary>
    public static class DoubleBufferDataGirdView
    {
        /// <summary>
        /// 使用双缓冲，解决 DataGridView 不闪烁
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="flag"></param>
        public static void DoubleBufferedDataGirdView(this DataGridView dgv, bool flag)
        {
            Type dgvType = dgv.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(dgv, flag, null);
        }
    }
    /// <summary>
    /// ListView 双缓冲
    /// </summary>
    public static class DoubleBufferListView
    {
        /// <summary>
        /// 使用双缓冲，解决 ListView 不闪烁
        /// </summary>
        /// <param name="lv"></param>
        /// <param name="flag"></param>
        public static void DoubleBufferedListView(this ListView lv, bool flag)
        {
            Type lvType = lv.GetType();
            PropertyInfo pi = lvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(lv, flag, null);
        }
    }
    /// <summary>
    /// DataGridView 初始化样式
    /// </summary>
    public static class DataGridViewInitStyle
    {
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void InitStyle(this DataGridView dataGridView)
        {
            dataGridView.ColumnHeadersDefaultCellStyle.Padding = new Padding(5);
            dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridView.ColumnHeadersDefaultCellStyle.SelectionForeColor = ColorSettings.DATAGRIDVIEW_HEADER_SELECTED_FORE_COLOR;
            dataGridView.ColumnHeadersDefaultCellStyle.SelectionBackColor = ColorSettings.DATAGRIDVIEW_HEADER_SELECTED_BACK_COLOR;
            dataGridView.ColumnHeadersDefaultCellStyle.BackColor = ColorSettings.DATAGRIDVIEW_HEADER_BACK_COLOR;
            dataGridView.ColumnHeadersDefaultCellStyle.ForeColor = ColorSettings.DATAGRIDVIEW_HEADER_FORE_COLOR;
            //dataGridView.DefaultCellStyle.SelectionForeColor = ColorSettings.DATAGRIDVIEW_CELL_SELECTED_FORE_COLOR;
            dataGridView.DefaultCellStyle.SelectionBackColor = ColorSettings.DATAGRIDVIEW_CELL_SELECTED_BACK_COLOR;
            dataGridView.DefaultCellStyle.BackColor = ColorSettings.DATAGRIDVIEW_CELL_DEFAULT_BACK_COLOR;
            dataGridView.DefaultCellStyle.ForeColor = ColorSettings.DATAGRIDVIEW_CELL_DEFAULT_FORE_COLOR;
            dataGridView.DefaultCellStyle.Font = ColorSettings.DATAGRIDVIEW_CELL_FONT;
            if (dataGridView.Columns != null && dataGridView.Columns.Count > 0)
            {
                foreach (DataGridViewColumn column in dataGridView.Columns)
                {
                    if (column is DataGridViewComboBoxColumn)
                    {
                        (column as DataGridViewComboBoxColumn).FlatStyle = FlatStyle.Flat;
                    }
                }
            }
            dataGridView.CellStateChanged += DataGridView_CellStateChanged;
        }
        /// <summary>
        /// 单元格状态变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void DataGridView_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            if (e.Cell == null || e.Cell.State != DataGridViewElementStates.Selected)
            {
                return;
            }
            else if (e.Cell != null && e.Cell.State == DataGridViewElementStates.Selected && e.Cell.ReadOnly)
            {
                e.Cell.Selected = false;
            }
        }
    }
}
